package com.ph.elec.gwt.post.client;

import java.util.Arrays;
import java.util.Date;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.ph.elec.data.FrontNews;
import com.ph.elec.gwt.util.UIUtils;
import com.ph.elec.gwt.util.UIUtils.StdDecoratorPanel;

public class FrontNewsPost implements EntryPoint {

	private static final DateTimeFormat dateFormat = DateTimeFormat.getFormat("M/dd/yyyy  h:m a");
	public PostPanel postPanel = new PostPanel();
	public final Button btnPost = new Button();

	public void onModuleLoad() {
		VerticalPanel vtpRoot = new VerticalPanel();

		vtpRoot.add(UIUtils.decorateBox(postPanel, "New Post"));

		vtpRoot.add(UIUtils.buttonArea(Arrays.asList(new Widget[] {
				UIUtils.decorateButton(btnPost, "Post!")
		})));
		btnPost.addClickHandler(new PostClickHandler());

		RootPanel panel = RootPanel.get("app");
		panel.add(vtpRoot);
		
	}

	protected class PostPanel extends StdDecoratorPanel {

		public TextBox txtTitle = new TextBox();
		public TextBox txtKeywords = new TextBox();
		public TextArea txaSnippet = new TextArea();
		public TextArea txaBody = new TextArea();
		public TextBox txtPoster = new TextBox();
		public TextBox txtPostTime = new TextBox();

		public PostPanel() {
			vtpRoot.add(UIUtils.decorateFieldName(
					UIUtils.decorateTextBox(txtTitle, "450px", 200), 
					"Title", 
					true));

			vtpRoot.add(UIUtils.decorateFieldName(
					UIUtils.decorateTextBox(txtKeywords, "450px", 300), 
					"Keywords  (separate with commas, for SEO -- not visible)", 
					false));

			vtpRoot.add(UIUtils.decorateFieldName(
					UIUtils.decorateTextArea(txaSnippet, "450px", 4), 
					"Snippet  (html)", 
					true));

			vtpRoot.add(UIUtils.decorateFieldName(
					UIUtils.decorateTextArea(txaBody, "450px", 8), 
					"Body Contents  (html)", 
					true));

			vtpRoot.add(UIUtils.decorateFieldName(
					UIUtils.decorateTextBox(txtPoster, "200px", 50), 
					"Poster", 
					true));

			vtpRoot.add(UIUtils.decorateFieldName(
					UIUtils.decorateTextBox(txtPostTime, "200px", 25), 
					"Post Time  (follow format)", 
					true));

			add(vtpRoot);
			setWidth("500px");
			
			initializeContents();
		}

		private void initializeContents() {
			txtPostTime.setText(dateFormat.format(new Date()));
		}
	}

	protected class PostClickHandler implements ClickHandler {
		
		boolean invalid = true;
		
		public void onClick(ClickEvent event) {
			Date postTime;
			try {
				postTime = dateFormat.parse(postPanel.txtPostTime.getText());
			} catch (IllegalArgumentException ex) {
				UIUtils.popAutoHideDialog("Invalid Post Time", 
						"The format of your Post Time is invalid.");
				return;
			}
			if (postPanel.txaBody.getText().isEmpty() ||
					postPanel.txaSnippet.getText().isEmpty() ||
					postPanel.txtPoster.getText().isEmpty() ||
					postPanel.txtTitle.getText().isEmpty()) {
				UIUtils.popAutoHideDialog("Empty Required Fields", 
				"All fields except for Keywords are required.");
				return;
			}

			FrontNews entry = new FrontNews();
			entry.setTimePost(NumberFormat.getFormat("T000000000000000").
					format(postTime.getTime()));
			entry.setBody(postPanel.txaBody.getText().trim());
			entry.setSnippet(postPanel.txaSnippet.getText().trim());
			entry.setKeywords(postPanel.txtKeywords.getText().trim());
			entry.setPoster(postPanel.txtPoster.getText().trim());
			entry.setTitle(postPanel.txtTitle.getText().trim());

			PostServiceAsync postSv = GWT.create(PostService.class);
			AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
				public void onFailure(Throwable caught) {
				}
				public void onSuccess(Boolean result) {
					Window.open("/page/index.htm", "_self", null); 
				}
			};
			postSv.FrontNews(entry, callback);
		}
	}

}
